<?php

namespace app\common\formula;

use app\model\RatingScaleFactorModel;

class Calculate
{
    /**
     * 获取用户因素得分
     * @param $questionScoreArr
     * @param $ratingScaleId
     * @param $modeId
     * @param $uid
     * @param $is_valid
     * @return array
     */
    public function getUserFactorScore($questionScoreArr, $ratingScaleId, $modeId, $uid,$is_valid): array
    {
        $ratingScaleFactorModel = new RatingScaleFactorModel();
        $factors = $ratingScaleFactorModel->field('id,name,formula_method,formula,warning,rating_scale_id')
            ->where([
                ['rating_scale_id', '=', $ratingScaleId],
                ['mode_id', '=', $modeId]
            ])
            ->with([
                'relatedQuestions' => function ($query) {
                    $query->with('questionForNum');
                },
                'ratingScalePreviousName'
            ])->select()->toArray();
        $factorResults = [];
        $factorName = ['量表效度', '效度因子', '效度因子', '说谎（L）', '疑问(Q)', '重复题', '掩饰、假托或自身隐蔽'];
        foreach ($factors as $factor) {
            $factorResults[$factor['id']] = [
                'factor_id' => $factor['id'],
                'factor_name' => $factor['name'],
                'score' => $this->FormulaMethod(
                    $factor['formula_method'],
                    $factor['relatedQuestions'],
                    $questionScoreArr,
                    $factor['formula'],
                    $uid
                )
            ];
            //用于算法十三。答题一致判断是否有效
            if ($factor['formula_method'] == 'formula13' && $factor['rating_scale_previous_name'] == '青少年(青春期)性心理健康量表' && $factorResults[$factor['id']]['score'] == 4) {
                $is_valid = 0;
            }
            //统计无效因子
            if (in_array($factor['name'], $factorName) && $factorResults[$factor['id']]['score'] >= $factor['warning']) {
                $is_valid = 0;
            }
            //90测谎题
            if ($factor['rating_scale_previous_name'] == '症状自评量表(SCL-90)' && $factor['formula_method'] == 'formula101' && $factorResults[$factor['id']]['score'] >= 8){
                $is_valid = 0;
            }
        }
        return [
            'factorResults' => $factorResults,
            'is_valid' => $is_valid
        ];
    }


    /**
     * 获取对应算法计算的值
     * @param $formulaStr
     * @param $relatedQuestionArr
     * @param $questionScoreArr
     * @param $formula
     * @param $uid
     * @return float|void
     */
    private function FormulaMethod($formulaStr, $relatedQuestionArr, $questionScoreArr, $formula, $uid)
    {
        $Formula = new Formula();
        switch ($formulaStr) {
            case "formula1":
                return $Formula->formula1($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula2":
                return $Formula->formula2($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula3":
                return $Formula->formula3($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula4":
                return $Formula->formula4($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula5":
                return $Formula->formula5($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula6":
                return $Formula->formula6($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula7":
                return $Formula->formula7($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula8":
                return $Formula->formula8($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula9":
                return $Formula->formula9($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula10":
                return $Formula->formula10($relatedQuestionArr, $questionScoreArr, $formula);
            case "formula11":
                return $Formula->formula11($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula12":
                return $Formula->formula12($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula13":
                return $Formula->formula13($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula14":
                return $Formula->formula14($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula15":
                return $Formula->formula15($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula16":
                return $Formula->formula16($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula17":
                return $Formula->formula17($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula18":
                return $Formula->formula18($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula19":
                return $Formula->formula19($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula20":
                return $Formula->formula20($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula21":
                return $Formula->formula21($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula101":
                return $Formula->formula101($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula102":
                return $Formula->formula102($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula103":
                return $Formula->formula103($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula104":
                return $Formula->formula104($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula105":
                return $Formula->formula105($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula106":
                return $Formula->formula106($relatedQuestionArr, $questionScoreArr, $formula, $uid);
            case "formula201":
                return $Formula->formula201($relatedQuestionArr, $questionScoreArr, $formula, $uid);
        }
        exit(error("没有这个计算方式，请联系管理员"));
    }
}